<template>
	<view class="content">
		<view class="top">
			<!-- v-if="isScroll==false" -->
			<view class="top_bg">
				<image src="https://img.jinghushi.com/images/9/2025/10/lm56t54Qj55RYJ43n3VrLjm53aajxr.png" ></image>
			</view>
			<view class="tit" :class="{'isScroll':isScroll}">
				<view class="arror_box" @click="$navBack()">
					<image class="arror" src="/static/home/r_arror_back.png" ></image>
				</view>
				<image class="g-t" src="/static/home/group_tit.png" mode="heightFix"></image>
			</view>
		</view>
		<view class="list_box">
			<view class="l_tit" :class="{'isTitScroll':isTitScroll}">
				<view class="main_tit" v-for="item,i in titList" @click="changTitTab(item.type)" :key="item.type">
					<text>{{item.name}}</text>
					<image v-if="item.type==activityTab" src="/static/home/line.png" ></image>
				</view>
			</view>
			<view class="time_box flex" v-if="activityTab!=1">
				<view class="tb_left" v-if="park_info.start_time">
					<text class="tit_time">开始时间:</text>
					<text class="time">{{park_info.start_time}}</text>
				</view>
				<view class="tb_left" v-if="park_info.end_time">
					<text class="tit_time">结束时间:</text>
					<text class="time">{{park_info.end_time}}</text>
				</view>
			</view>
			<view class="goods_list">
				<view class="gl_box flex" v-for="item,i in list" @click.stop="goDetails(item)">
					<view class="left">
						<image :src="item.goods_info.thumb"></image>
					</view>
					<view class="right flex">
						<view class="">
							<view class="g_tit">
								{{item.goods_name}}
							</view>
							<view class="show_num flex_between">
								<view class="group_now">
									<image src="/static/home/group_buying.png"></image>
									<text v-if="activityTab==1">{{item.join_num}}人拼过</text>
									<text v-else-if="activityTab==2">{{item.join_num}}人正在拼团</text>
									<text v-else-if="activityTab==3">待开团</text>
								</view>
								<view class="num" v-if="activityTab==3">
									即将开售
								</view>
								<view class="num" v-else>
									成团数量{{item.group_num}}
								</view>
							</view>
						</view>

						<view class="bottom flex">
							<view class="price">
								￥{{item.group_price}}
							</view>
							<view class="btn" v-if="activityTab==2">
								<view class="b_box" @click.stop="groupFormation(item)">
									<image src="/static/home/none_group.png"></image>
									<text class="none_group">一键成团</text>
								</view>
								<view class="b_box">
									<image src="/static/home/grouop.png"></image>
									<text class="grouop">立即拼团</text>
								</view>
							</view>
							<view class="btn2" v-else>
								<text v-if="activityTab==1">已过期</text>
								<text v-if="activityTab==3">待开团</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- -->
			<view class="empty_loading">
				<image v-if="isloading" src="/static/loading.gif"></image>
				<text v-if="isempty">没有更多了~</text>
			</view>
		</view>
		<groupPopup v-if="popu" :type="groupType" :goodsid="goodsid" :parkId="parkId" @cler="clet" />
		<!-- <groupPopup v-if="popu" :info="info" :ids="shopid" :ok="ok" @cler="clet" :gui="gui?'1':'0'" /> -->
		<uni-popup ref="popupMobile" :is-mask-click="true" :z-index="1">
			<view class="nobind">
				<view>您还未绑定手机号</view>
				<image src="/static/close.png" @click="closeBind" mode="widthFix" class="e_close"></image>
				<!-- #ifdef MP-WEIXIN -->
				<button class="l_w_login" open-type="getPhoneNumber" @getphonenumber="getUserProfile">一键绑定手机号</button>
				<!-- #endif -->
				<!-- #ifdef MP-ALIPAY -->
				<button class="l_w_login" open-type="getAuthorize" scope="phoneNumber" @getAuthorize="getUserProfile"
					onError="handleAuthError">一键绑定手机号</button>
				<!-- #endif -->
			</view>
		</uni-popup>
		<bar></bar>
	</view>

</template>

<script>
	import groupPopup from './components/popup.vue';
	import bar from './components/bar.vue';
	export default {
		components: {
			groupPopup,bar
		},
		data() {
			return {
				titList: [{
						name: '历史拼团',
						type: 1
					},
					{
						name: '正在拼团',
						type: 2
					},
					{
						name: '拼团预告',
						type: 3
					}
				],
				activityTab: 2,
				isScroll: false,
				isTitScroll: false,
				popu: false,
				info: [],
				list: [],
				isloading: false,
				isempty: false,
				page: 0,
				park_info: {},
				goodsid: '',
				parkId: '',
				groupType: '',
				isRequesting: false, // 请求锁：防止同一时间发多个请求
				currentRequestKey: '' // 当前请求标识：标记“正在等待响应的请求属于哪个Tab/页码”

			}
		},
		onLoad() {
			this.getList();
		},
		onPageScroll(e) {
			this.isScroll = e.scrollTop > 0
			this.isTitScroll = e.scrollTop > 145
		},
		onReachBottom() {
			this.getList();
		},
		methods: {
			// 绑定手机号
			getUserProfile(e) {
				// console.log(e);
				// return;
				let that = this;
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let obj = e.detail;
						obj.code1 = loginRes.code;
						that.$axios('bindmobile/bmobile', "POST", 'member', obj).then(res => {
							if (res.data.code == 200) {
								that.$refs.popupMobile.close();
							}
						})
					},
					fail() {
						that.$api.msg('绑定失败，请重试！')
					}
				})
				// #endif
			},
			closeBind() {
				this.$refs.popupMobile.close();
			},
			groupFormation(item) {
				this.$axios('Info/member_info', "POST", 'member').then(res => {
					if (res.data.code = 200) {
						if (res.data.data.member.mobile == '') {
							this.$refs.popupMobile.open();
						} else {
							this.groupType = 'debutAgroup'
							this.goodsid = item.goodsid
							this.parkId = item.park_id
							this.popu = true;
							this.info = item;
						}
					}
				})
			},
			getList() {
				if (this.isRequesting || this.isempty) return;
				const requestKey = `${this.activityTab}_${this.page + 1}`;
				this.currentRequestKey = requestKey;
				this.page++;
				this.isloading = true;
				this.isRequesting = true;
				this.$axios('Group/goodsSearch', 'POST', 'group_buy', {
					page: this.page,
					type: this.activityTab
				}).then(res => {
					if (this.currentRequestKey !== requestKey) {
						console.log('忽略无效响应：', requestKey);
						return;
					}
					this.isloading = false;
					this.isRequesting = false;
					if (res.data.code === 200) {
						const newList = res.data.data.list || [];
						this.list = [...this.list, ...newList];
						this.park_info = res.data.data.park_info || {};
						this.isempty = newList.length <= 0;
					} else {
						this.page--;
						this.isempty = false;
					}
				}).catch(err => {
					if (this.currentRequestKey === requestKey) {
						this.isloading = false;
						this.isRequesting = false;
						this.page--;
						uni.showToast({
							title: '加载失败',
							icon: 'none'
						});
					}
				});
			},
			clet() {
				this.popu = false
			},
			goDetails(item) {
				uni.navigateTo({
					url: '/pages/pintuan/product?goodsid=' + item.goodsid + '&parkId=' + item.park_id +
						'&status=' + this.activityTab
				})
			},

			changTitTab(i) {
				this.activityTab = i;
				this.page = 0;
				this.list = []; // 清空当前列表
				this.isloading = false;
				this.isempty = false;
				this.isRequesting = false;
				this.currentRequestKey = '';

				// 2. 发起新Tab的请求
				this.getList();
			},
			ceshi() {
				// this.$axios('Util/categorynew','POST','shop').then(res=>{
				// })
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fdf8ff;
	}

	.nobind {
		position: relative;

		.e_close {
			width: 30rpx;
			height: 30rpx;
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}
	}

	.flex {
		display: flex;
		justify-content: space-between;
	}

	.list_box {
		position: relative;
		top: -36rpx;
		border-radius: 30rpx 30rpx 0 0;
		background-color: #fdf8ff;
		z-index: 88;
		overflow: hidden;
		padding-bottom: 60rpx;

		.goods_list {
			padding: 20rpx;

			// background-color: pink;
			.right {
				flex-direction: column;
				width: 458rpx;

				.bottom {
					align-items: center;

					.price {
						color: #9A1FE8;
						font-size: 32rpx;
						font-weight: bold;
					}

					.btn {
						display: flex;

						.b_box {
							width: 136rpx;
							height: 60rpx;
							position: relative;

							image {
								width: 100%;
								height: 100%;
							}

							text {
								position: absolute;
								width: 136rpx;
								height: 60rpx;
								left: 0;
								top: 0;
								text-align: center;
								line-height: 60rpx;
								font-weight: bold;
								font-size: 24rpx;
							}

							.none_group {
								color: #9A1FE8;
							}

							.grouop {
								color: #FFFFFF;
							}
						}
					}

					.btn2 {
						width: 176rpx;
						height: 60rpx;
						line-height: 60rpx;
						text-align: center;
						background: #9A1FE8;
						border-radius: 144rpx;
						color: #FFFFFF;
						font-weight: bold;
					}
				}

				.show_num {
					color: #9A1FE8;
					margin-top: 15rpx;

					.num {
						font-size: 20rpx;
						color: #9A1FE8;
						font-weight: bold;
					}

					.group_now {
						display: flex;
						align-items: center;

						image {
							width: 120rpx;
							height: 36rpx;
							z-index: 99;
						}

						text {
							background-color: #f5e9fd;
							font-size: 20rpx;
							height: 36rpx;
							line-height: 36rpx;
							padding: 0 20rpx;
							border-radius: 8rpx;
							margin-left: -20rpx;
						}
					}
				}

				.g_tit {
					width: 100%;
					color: #13001E;
					font-size: 28rpx;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					/* 限制显示的行数 */
					-webkit-box-orient: vertical;
					overflow: hidden;
				}
			}

			.gl_box {
				border-radius: 24rpx;
				background: #FFFFFF;
				padding: 20rpx;
				width: 100%;
				height: 240rpx;
				margin-bottom: 20rpx;

				.left {
					image {
						width: 200rpx;
						height: 200rpx;
						border-radius: 8rpx;
					}
				}
			}
		}

		.time_box {
			width: 100%;
			padding: 0 20rpx;
			// height: 60rpx;
			align-items: center;

			.tb_left {
				text {
					font-size: 24rpx;
				}

				.tit_time {
					color: #13001E;
					font-weight: normal;
				}

				.time {
					color: #9A1FE8;
					font-weight: bold;
				}
			}
		}

		.l_tit {
			width: 100%;
			display: flex;
			justify-content: space-evenly;
			padding: 24rpx 0;
			background: #FDF8FF;

			.main_tit {
				display: inline-block;
				position: relative;

				text {
					font-weight: bold;
					color: #13001E;
					font-size: 28rpx;
				}

				image {
					position: absolute;
					height: 12rpx;
					bottom: 0;
					left: 0;
					width: 100%;
					opacity: .8;
				}
			}
		}
	}

	.top {
		.top_bg {
			height: 450rpx;
			width: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.tit {
			position: absolute;
			height: 95px;
			top: 0;
			left: 0;
			// background-color: pink;
			width: 100%;
			display: flex;

			padding: calc(var(--status-bar-height) + 50rpx) 30rpx 0 0;
			padding-bottom: 0;

			.arror_box {
				width: 80rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				.arror {
					width: 18rpx;
					height: 34rpx;
				}
			}

			.g-t {
				height: 60rpx;
			}
		}
	}

	.tit.isScroll {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		/* 确保宽度满屏 */
		height: 110px;
		background-color: yellow;
		z-index: 9999;
		/* 提高层级，避免被遮挡 */
		padding: calc(var(--status-bar-height) + 50rpx) 30rpx 0 0;
		padding-bottom: 15px;
		/* 调整内边距 */
		box-sizing: border-box;
		/* 确保宽度包含内边距 */
		background: linear-gradient(180deg, #9A1FE8 0%, #FFFFFF 100%);
		border-bottom: 2px solid #CCCACD;
	}

	.isTitScroll {
		position: fixed;
		top: 110px;
		z-index: 9999;
		/* 提高层级，避免被遮挡 */
	}
</style>